import { Controller, Get, Query, Param, Body, Put } from '@nestjs/common'
import { StudentService } from './student.service'
import { Student } from './student.entity'

@Controller('student')
export class StudentController {
	constructor(private readonly studentService: StudentService) { }

	@Get()
	root(): string {
		return this.studentService.root()
	}

	@Get('save')
	save(@Query() query): any {

		console.log(query)
		let student = new Student()
		student.name = query.name
		student.age = Number(query.age)
		if (query.id) {
			student.id = Number(query.id)
		}

		return this.studentService.save(student)
	}
	
	@Get('get')
	get(@Query() query): any {

		if (query.id) {
			query.id = Number(query.id)
		}
		if (query.age) {
			query.age = Number(query.age)
		}
		return this.studentService.get(query)
	}
	
	@Put("update")
	async update(@Body() post) {
		if (post.id) {
			post.id = Number(post.id)
		}
		return await this.studentService.updateById(post)
	}
	
	@Get('remove')
	remove(@Query() query): any {

		if (query.id) {
			query.id = Number(query.id)
		}
		if (query.age) {
			query.age = Number(query.age)
		}
		return this.studentService.remove(query)
	}
}